package com.huizhonglm.diap.ui.views;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.huizhonglm.diap.R;
import com.huizhonglm.diap.core.blur.FastBlur;
import com.huizhonglm.diap.core.utils.DisplayUtil;


/**
 * 弹出框、下拉菜单模态背景
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2013年9月5日上午8:58:08
 */
public class PopupMaskView extends PopupWindow {

    private Activity mActivity;
    private ImageView image;
    private View trans;
    private RelativeLayout contentView;
    private PopupWindow mMenu;

    private int bmpWidth;
    private int bmpHeight;
    private int barHeight;


    public PopupMaskView(Activity activity, final PopupWindow menu) {
        super(activity);
        this.mMenu = menu;
        this.mActivity = activity;
        image = new ImageView(activity);
        contentView = new RelativeLayout(mActivity);
        trans = new View(mActivity);
        trans.setBackgroundColor(0x70212121);
        // 获取状态栏高度
        barHeight = getStatusBarHeight();
        // 获取屏幕长和高
        bmpWidth = DisplayUtil.getWidth();
        bmpHeight = DisplayUtil.getHeight();
        init();
    }

    public void showAtLocation(View parent) {
        Bitmap bmp = takeScreenShot();
        if (bmp != null) {
            blur(bmp, image);
            if (!this.isShowing()) {
                this.showAtLocation(parent, Gravity.NO_GRAVITY, 0, barHeight);
            }
        }
    }

    private void init() {
        image.setLayoutParams(new RelativeLayout.LayoutParams(bmpWidth, bmpHeight));
        trans.setLayoutParams(new RelativeLayout.LayoutParams(bmpWidth, bmpHeight));
        contentView.setLayoutParams(new ViewGroup.LayoutParams(bmpWidth, bmpHeight));
        contentView.addView(image);
        contentView.addView(trans);
        this.setContentView(contentView);
        this.setWidth(bmpWidth);
        this.setHeight(bmpHeight);
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setTouchable(true);
        this.setOutsideTouchable(true);
        this.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (mMenu.isShowing()) {
                    mMenu.dismiss();
                    dismiss();
                }
                return true;
            }
        });
        this.setAnimationStyle(R.style.popup_mask_anim_style);
        update();
    }

    private void blur(Bitmap bkg, ImageView view) {
        float scaleFactor = 2;//缩放图片，缩放之后模糊效果更好
        float radius = 2;// 模糊半径
        int w = (int) (bmpWidth / scaleFactor);
        int h = (int) (bmpHeight / scaleFactor);
        Bitmap overlay = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(overlay);
        canvas.translate(-view.getLeft() / scaleFactor, -view.getTop() / scaleFactor);
        canvas.scale(1 / scaleFactor, 1 / scaleFactor);
        Paint paint = new Paint();
        paint.setFlags(Paint.FILTER_BITMAP_FLAG);
        canvas.drawBitmap(bkg, 0, 0, paint);
        overlay = FastBlur.doBlur(overlay, (int) radius, true);
        view.setImageDrawable(new BitmapDrawable(mActivity.getResources(), overlay));
    }

    private Bitmap takeScreenShot() {
        // View是你需要截图的View
        View view = mActivity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bm = view.getDrawingCache();
        Bitmap bmp = Bitmap.createBitmap(bm, 0, 0, bmpWidth, bmpHeight);
        view.destroyDrawingCache();
        return bmp;
    }

    /**
     * 获取状态栏高度
     *
     * @return 状态栏高度
     */
    private int getStatusBarHeight() {
        Rect frame = new Rect();
        mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        return frame.top;
    }
}
